import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'

// GET /api/courses/:id - 获取单个课程
export async function GET(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        const course = await prisma.course.findUnique({
            where: { id },
            include: { orders: true }
        })

        if (!course) {
            return NextResponse.json(
                { success: false, error: 'Course not found' },
                { status: 404 }
            )
        }

        return NextResponse.json({
            success: true,
            data: course
        })
    } catch (error) {
        console.error('Error fetching course:', error)
        return NextResponse.json(
            { success: false, error: 'Failed to fetch course' },
            { status: 500 }
        )
    }
}

// PUT /api/courses/:id - 更新课程
export async function PUT(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        const body = await request.json()

        const {
            name,
            original_price,
            main_pic,
            detail_pic,
            current_price,
            introduction
        } = body

        const course = await prisma.course.update({
            where: { id },
            data: {
                ...(name && { name }),
                ...(original_price && { original_price: Number(original_price) }),
                ...(main_pic !== undefined && { main_pic }),
                ...(detail_pic !== undefined && { detail_pic }),
                ...(current_price && { current_price: Number(current_price) }),
                ...(introduction !== undefined && { introduction })
            }
        })

        return NextResponse.json({
            success: true,
            data: course
        })
    } catch (error) {
        console.error('Error updating course:', error)
        return NextResponse.json(
            { success: false, error: 'Failed to update course' },
            { status: 500 }
        )
    }
}

// DELETE /api/courses/:id - 删除课程
export async function DELETE(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        await prisma.course.delete({
            where: { id }
        })

        return NextResponse.json({
            success: true,
            message: 'Course deleted successfully'
        })
    } catch (error) {
        console.error('Error deleting course:', error)
        return NextResponse.json(
            { success: false, error: 'Failed to delete course' },
            { status: 500 }
        )
    }
}
